import React, { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import Store from './../redux/store'
import * as actions from "./../redux/actions"
const Login = () => {
  const navigate = useNavigate();
  const [token, setToken] = useState(Store.getState().a.token)
  useEffect(() => {
    Store.subscribe(() => {
      setToken(Store.getState().a.token)
    });
  });
  // useEffect可以多次使用
  // 为了实现，登录成功后跳转页面
  useEffect(() => {
    // 也可以做jwt-token的验证
    if (token !== '') {
      navigate("/home")
    }
  }, [token]);

  return (
    <div>
      <h1>log in</h1>
      <button onClick={() => {
        // navigate("/home")
        Store.dispatch(actions.userLogin({ username: 'admin', password: '123456' }))


      }}>登录</button>
    </div>
  );
}

export default Login;
